package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.MyEnum.NumberConstant;
import com.example.Utility.RedisUtils;
import com.example.Utility.Result;
import com.example.Utility.objIsNull;
import com.example.entity.Goods;
import com.example.entity.HeadType;
import com.example.mapper.GoodsMapper;
import com.example.service.GoodsService;
import com.example.service.HeadTypeSrvice;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Slf4j
@Configuration
@RestController
@RequestMapping("/goods")
public class GoodsController implements WebMvcConfigurer {

    @Autowired
    private RedisUtils redisUtils;
    @Resource
    private GoodsService goodsService;
    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private HeadTypeSrvice headTypeSrvice;

    @Value("${network.path}")
    private String this_path;

    /**
     * 根据id获取商品数据
     *
     * @param Long id
     * @return Goods
     */
    @GetMapping("get")
    public Goods get(@RequestParam("id") Long id) {
        // 创建两个图片集合
        List slideshowList = new ArrayList<>();
        List detailsList = new ArrayList<>();
        Goods goodsInfo = goodsMapper.selectById(id);
        // 查询所有轮播图
        String slideshowPath = String.format("%s%s/轮播图/", this_path, goodsInfo.getNames());
        try (Stream<Path> paths = Files.walk(Paths.get(slideshowPath))) {
            paths.filter(Files::isRegularFile)
                    .forEach(path -> slideshowList.add(path.getFileName().toString()));
        } catch (IOException e) {
            // 处理异常
            log.error("文件处理异常");
        }
        goodsInfo.setSlideshow(slideshowList);
        String detailsPath = String.format("%s%s/详情介绍/", this_path, goodsInfo.getNames());
        try (Stream<Path> paths = Files.walk(Paths.get(detailsPath))) {
            paths.filter(Files::isRegularFile)
                    .forEach(path -> detailsList.add(path.getFileName().toString()));
        } catch (IOException e) {
            // 处理异常
            log.error("文件处理异常");
        }
        goodsInfo.setDetails(detailsList);
        return goodsInfo;
    }

    /**
     * 获取商品
     *
     * @return List<HeadType>
     */
    @Transactional
    @GetMapping("/lists")
    public Result<List<HeadType>> list() {
        List<HeadType> headTypes = redisUtils.getRedisObjectList("goodsList", HeadType.class);
        if (headTypes == null || headTypes.isEmpty()) {
            try (Stream<HeadType> stream = headTypeSrvice.list().stream()) {
                headTypes = stream.peek(headType -> {
                    List<Goods> subType = goodsService.list(new LambdaQueryWrapper<Goods>().eq(Goods::getHead_type, headType.getName()));
                    headType.setSubType(subType);
                }).collect(Collectors.toList());
            }

            if (!headTypes.isEmpty() && "所有商品".equals(headTypes.get(NumberConstant.ZERO).getName())) {
                headTypes.get(NumberConstant.ZERO).setSubType(goodsService.list());
            }
            redisUtils.redisAdd("goodsList", headTypes, NumberConstant.ONE, TimeUnit.MINUTES);
        }
        return Result.succeed(headTypes);
    }


    /**
     * 条件查询 id
     */
    @GetMapping("/like")
    public List<Goods> selectList(@RequestParam("indexName") String indexName) {
        String names = indexName;
        if (objIsNull.isNull(names)) {
            return goodsService.list();
        }
        List<Goods> goodsList = goodsMapper.selectList(new LambdaQueryWrapper<Goods>()
                .like(Goods::getNames, names)
                .or()
                .like(Goods::getHead_type, names)
                .or()
                .like(Goods::getIntroduce, names));
        return goodsList.isEmpty() ? goodsService.list() : goodsList;
    }

}
